package cn.bugDance.domain.activity.service.rule.impl;

import cn.bugDance.domain.activity.model.entity.ActivityCountEntity;
import cn.bugDance.domain.activity.model.entity.ActivityEntity;
import cn.bugDance.domain.activity.model.entity.ActivitySkuEntity;
import cn.bugDance.domain.activity.model.valobj.ActivityStateVO;
import cn.bugDance.domain.activity.service.rule.AbstractRuleChain;
import cn.bugDance.types.enums.ResponseCode;
import cn.bugDance.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component("ruleActivityStateChain")
@Slf4j
public class RuleActivityStateChain extends AbstractRuleChain {


    @Override
    public void action(ActivityEntity activityEntity, ActivitySkuEntity activitySkuEntity, ActivityCountEntity activityCountEntity) {

        Date beginTime = activityEntity.getBeginTime();
        Date endTime = activityEntity.getEndTime();
        Date nowTime = new Date();

        ActivityStateVO activityStateVO = activityEntity.getState();


        if(activityStateVO.equals(ActivityStateVO.REMOVE) || activityStateVO.equals(ActivityStateVO.CREATE)){
            log.info("【活动服务层-RuleActivityStateChain】: 活动未部署或者已经暂停");
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        if(nowTime.before(beginTime) || nowTime.after(endTime)){
            log.info("【活动服务层-RuleActivityStateChain】: 未在活动时间内");
            throw new AppException(ResponseCode.ACTIVITY_TIME_ERROR.getCode(), ResponseCode.ACTIVITY_TIME_ERROR.getInfo());
        }

        this.getNext().action(activityEntity, activitySkuEntity, activityCountEntity);
    }
}
